<!--  -->
<template>
  <div class='list-tree'>
    <div class="top">
      <el-input
        :placeholder="treePlaceholder"
        class="filter-input"
        v-model="filterText"
        suffix-icon="el-icon-search"
      >
      </el-input>
    </div>

    <div class="bottom">
      <el-tree
        node-key="ip"
        :props="defaultProps"
        :check-on-click-node="true"
        @node-click="handleNodeClick"
        :filter-node-method="filterNode"
        :default-checked-keys="checkDefault"
        :data="list"
        icon-class="none"
        ref="tree"
      >
        <div
          class="custom-tree-icon"
          slot-scope="{ data }"
        >
          <img
            class="host-img"
            src="/static/image/icon_host.png"
            alt=""
          />
          <span>{{ data[defaultProps.label] }}</span>
        </div>
      </el-tree>
    </div>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';

export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  props: {
    treePlaceholder: {
      type: String,
      default: "请输入关键词",
    },
    list: {
      type: Array,
      default: [],
    },
    checkDefault: {
      type: Array,
      default: [],
    },
    defaultProps: {
      type: Object,
      default: {
        children: "children",
        label: "label",
      },
    },
  },
  data() {
    //这里存放数据
    return {
      filterText: "",
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    },
    checkDefault: function (newVal, oldVal) {
      if (newVal) {
        this.$nextTick(() => {
          document.querySelector(".el-tree-node__content").click();
        });
      }
    },
  },
  //方法集合
  methods: {
    handleNodeClick(data, node, instance) {
      this.$emit("list-node-click", { data, node, instance });
    },
    filterNode(value, data) {
      if (!value) return true;
      return data[this.defaultProps.label].indexOf(value) !== -1;
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang='less' scoped>
//@import url(); 引入公共css类
.list-tree {
  display: flex;
  flex-direction: column;
  height: 100%;
  .top {
    display: flex;
    justify-content: center;
    margin: 0 16px;
    ::v-deep .el-input__inner {
      border: 1px solid #4864bd;
      background: rgba(89, 165, 248, 0.2);
      color: RGBA(123, 182, 255, 1);
    }
    .filter-input {
      width: 100%;

      margin-top: 29px;
      margin-bottom: 24px;
    }
  }
  .bottom {
    overflow: auto;
    // height: 500px;
    margin-bottom: 32px;
    flex: 1;
    ::v-deep .el-tree {
      height: 780px;

      background-color: transparent;
      font-size: 16px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      color: #9bcbff;
      .el-tree-node__content {
        height: 32px;
      }
      .el-tree-node:focus > .el-tree-node__content {
        background-color: rgba(255, 172, 65, 0.3);
      }
      .el-tree-node__content:hover {
        background-color: rgba(255, 172, 65, 0.3);
      }
      .el-tree-node.is-current > .el-tree-node__content {
        background-color: rgba(255, 172, 65, 0.3);
        color: #9bcbff;
      }
      .el-tree--highlight-current
        .el-tree-node.is-current
        > .el-tree-node__content {
        background-color: rgba(255, 172, 65, 0.3);
      }
    }
    .custom-tree-icon {
      display: flex;
      align-items: center;
      .host-img {
        margin-right: 8px;
      }
    }
  }
}
</style>